package com.kangfawei.thread;

/**
 * @author 康发伟
 * 模拟售票，线程安全性问题
 */
public class SaleTIcket implements Runnable {
    private Integer count = 100;

    @Override
    public void run() {
        while(count > 0){
            sale();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void sale(){
        if(count > 0){
            System.out.println(Thread.currentThread().getName() + "卖出了第" + (100-count+1) + "张票！");
            count -- ;
        }
    }

    public static void main(String[] args) {
        SaleTIcket saleTIcket = new SaleTIcket();
        new Thread(saleTIcket,"售票员一").start();
        new Thread(saleTIcket,"售票员二").start();
        new Thread(saleTIcket,"售票员三").start();
        new Thread(saleTIcket,"售票员四").start();
    }
}
